﻿using System;
using System.Collections.Generic;
using System.DirectoryServices;
using OutlookContactsSync.Domain.Model;
using OutlookContactsSync.Domain.Properties;
using OutlookContactsSync.Domain.Repository;

namespace OutlookContactsSync.Repository
{
    public class ContactRepository : BaseRepository, IContactRepository
    {
        #region IContactRepository Members

        public List<Contact> GetDomainUsers(ref bool cancellationPending)
        {
            ReportProgressBack(3, 1);

            var domainUsers = new List<Contact>();

            // Get Doamin users
            var searcher = new DirectorySearcher
                               {
                                   Filter = Settings.Default.Filter
                               };

            searcher.PropertiesToLoad.Add("mobile"); // +31 (6) 12345678
            searcher.PropertiesToLoad.Add("company"); // NETHERLANDS
            searcher.PropertiesToLoad.Add("wwwhomepage"); // http://my/sites/username/
            searcher.PropertiesToLoad.Add("physicaldeliveryofficename"); // SCHIPHOL-EVERT/5112
            searcher.PropertiesToLoad.Add("givenname"); // FirstName
            searcher.PropertiesToLoad.Add("manager");
            // CN=My Manager,OU=UserAccounts,DC=europe,DC=corp,DC=contoso,DC=com
            searcher.PropertiesToLoad.Add("samaccountname"); // myaccount
            searcher.PropertiesToLoad.Add("telephonenumber"); // +31 (20) 1234567 X4567
            searcher.PropertiesToLoad.Add("thumbnailphoto");
            searcher.PropertiesToLoad.Add("department"); // NL-MY DEPARTMENT
            searcher.PropertiesToLoad.Add("displayname"); // FirstName LastName
            searcher.PropertiesToLoad.Add("facsimiletelephonenumber"); // +31 (20) 1239876
            searcher.PropertiesToLoad.Add("mail"); // firstname.lastname@contoso.com
            searcher.PropertiesToLoad.Add("title"); // JOBTITLE
            searcher.PropertiesToLoad.Add("sn"); // LastName

            searcher.ClientTimeout = TimeSpan.FromSeconds(DefaultSettings.Default.ADTimeOutInSeconds);
            SearchResultCollection users = searcher.FindAll();
            
            ReportProgressBack(3, 2);

            // Add Domain users to List
            domainUsers.Clear();

            for (int i = 0; i < users.Count; i++)
            {
                if(cancellationPending)
                {
                    return null;
                }

                var item = new Contact();

                if (users[i].Properties.Contains("mobile"))
                    item.MobileTelephoneNumber = users[i].Properties["mobile"][0].ToString();
                if (users[i].Properties.Contains("company"))
                    item.CompanyName = users[i].Properties["company"][0].ToString();
                if (users[i].Properties.Contains("wwwhomepage"))
                    item.WebPage = users[i].Properties["wwwhomepage"][0].ToString();
                if (users[i].Properties.Contains("physicaldeliveryofficename"))
                    item.OfficeLocation = users[i].Properties["physicaldeliveryofficename"][0].ToString();
                if (users[i].Properties.Contains("givenname"))
                    item.FirstName = users[i].Properties["givenname"][0].ToString();
                if (users[i].Properties.Contains("manager"))
                    item.ManagerName = users[i].Properties["manager"][0].ToString();
                if (users[i].Properties.Contains("samaccountname"))
                    item.Account = users[i].Properties["samaccountname"][0].ToString();
                if (users[i].Properties.Contains("telephonenumber"))
                    item.BusinessTelephoneNumber = users[i].Properties["telephonenumber"][0].ToString();
                if (users[i].Properties.Contains("department"))
                    item.Department = users[i].Properties["department"][0].ToString();
                if (users[i].Properties.Contains("displayname"))
                    item.FullName = users[i].Properties["displayname"][0].ToString();
                if (users[i].Properties.Contains("facsimiletelephonenumber"))
                    item.BusinessFaxNumber = users[i].Properties["facsimiletelephonenumber"][0].ToString();
                if (users[i].Properties.Contains("mail"))
                    item.Email1Address = users[i].Properties["mail"][0].ToString();
                if (users[i].Properties.Contains("title")) item.JobTitle = users[i].Properties["title"][0].ToString();
                if (users[i].Properties.Contains("sn")) item.LastName = users[i].Properties["sn"][0].ToString();
                if (users[i].Properties.Contains("samaccountname"))
                    item.NickName = users[i].Properties["samaccountname"][0].ToString();
                if (users[i].Properties.Contains("thumbnailphoto"))
                    item.Picture = (byte[]) users[i].Properties["thumbnailphoto"][0];

                domainUsers.Add(item);
            }

            ReportProgressBack(3, 3);

            return domainUsers;
        }

        #endregion
    }
}